<?php
// +----------------------------------------------------------------------
// | Author: wyd 2025/10/19
// | DESC: 
// +----------------------------------------------------------------------

declare (strict_types=1);

use My\core\annotations\MethodAnnotation;

return [
    // [路由]方法属性注解
    MethodAnnotation::class => function (\ReflectionMethod $method, object $instance, array $args) {

        $routerCollector = \My\core\BeanFactory::getBean(\My\core\frame\RouterCollector::class);
        $routerCollector->addRouter($args['method']??'GET',$args['uri'],function ($param, $extParams) use ($method, $instance){
            //返回给路由框架,路由框架可以捕获并使用这个返回值（如作为HTTP响应内容）
            //return  $method->invoke($instance); //执行反射方法
            $refParams = $method->getParameters(); //得到反射方法的参数；
            $ableParams = [];
            foreach ($refParams as $refParam){
                $paramFound = false;

                if (isset($param[$refParam->getName()])) {
                    $ableParams[] = $param[$refParam->getName()];
                    $paramFound = true;
                } else {
                    foreach ($extParams as $extParam) {
                        if ($refParam->getType() && $extParam instanceof ($refParam->getType()->getName())) {
                            $ableParams[] = $extParam;
                            $paramFound = true;
                            break; // 跳出内层循环
                        }
                    }

                    if (!$paramFound) {
                        $ableParams[] = false;
                    }
                }
            }
            //fastroute 规定必须return;
            return   $method->invokeArgs($instance,$ableParams);
        });
    }
];